//
//  ZSTabBar.swift
//  新浪微博-Swift
//
//  Created by 周李波 on 16/7/19.
//  Copyright © 2016年 q. All rights reserved.
//

import UIKit
//MARK:- 设置代理
protocol ZSTabBarDelegate:NSObjectProtocol {
    func composeButtonClick()
}

class ZSTabBar: UITabBar {
    //设置代理
    weak var zsDelegate:ZSTabBarDelegate?
    
    
    //MARK:- 按钮点击事件
    func buttonClick(){
        zsDelegate?.composeButtonClick()
    }

    //MARK:- 初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:- 设置UI
    func setupUI(){
        addSubview(composeButton)
    }
    //MARK:- 设置约束
    override func layoutSubviews() {
        super.layoutSubviews()
        let width = ScreenWidth * 0.2
        var index:CGFloat = 0
        for value in subviews {
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                //设置frame
                value.frame.origin.x = index * width
                value.frame.size.width = width
                
                index++
                //中间空一格
                if index == 2 {
                    index++
                }
                
            }
            
        }
        
        //设置中间按钮
        composeButton.center.x = frame.width * 0.5
        composeButton.center.y = frame.height * 0.5

    }
    //MARK:- 懒加载控件
    lazy var composeButton: UIButton = {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.sizeToFit()
        //添加点击事件
        button.addTarget(self, action: #selector(ZSTabBar.buttonClick), forControlEvents: .TouchUpInside)
        return button
    }()
}
